﻿using System;
using System.IO;
using System.Text.RegularExpressions;

class FileSplitter
{
    public const int PART_SIZE = 1024 * 1024;

    public static void SavePart(byte[] bytesToSave,int readBytesCount, string fileName, string password)
    {
        using (FileStream part = new FileStream(fileName, FileMode.OpenOrCreate,FileAccess.Write))
        {
            byte[] encryptedBytes = Rijndael.EncryptBytes(bytesToSave, password);
            part.Write(encryptedBytes, 0, encryptedBytes.Length);
            //part.Write(bytesToSave, 0, readBytesCount);
        }
    }

    public static void SplitToParts(string path, string password)
    {
        using (FileStream source = new FileStream(path, FileMode.Open))
        {
            int partsCount = 0;
            FileInfo file = new FileInfo(path);
            while (true)
            {
                byte[] readBytes = new byte[PART_SIZE];
                int readBytesCount = source.Read(readBytes, 0, PART_SIZE);                
                if (readBytesCount == 0)
                {
                    break;
                }
                string fileName = String.Format(@"{0}\{1}-part{2:D4}{3}", file.DirectoryName, file.Name.Substring(0, file.Name.IndexOf(".")), partsCount, file.Extension);
                Console.WriteLine(fileName);
                SavePart(readBytes, readBytesCount, fileName, password);
                partsCount++;
            }
        }
    }

    public static void CombineParts(string path, string password)
    {
        string newFileName = path.Substring(0,path.IndexOf('.'));
        FileInfo file = new FileInfo(path);
        DirectoryInfo dir = new DirectoryInfo(file.DirectoryName);
        using (FileStream output = new FileStream(newFileName + "-original" + file.Extension, FileMode.Create))
        {
            string filePattern = "-part0";
            foreach (FileInfo currentFile in dir.GetFiles())
            {
                Match m = Regex.Match(currentFile.Name, filePattern);
                if (m.Success)
                {
                    using (FileStream part = new FileStream(currentFile.FullName, FileMode.Open))
                    {
                        byte[] bytes = new byte[part.Length];
                        part.Read(bytes, 0, bytes.Length);
                        byte[] dectyptedBytes = Rijndael.DecryptBytes(bytes, password);
                        output.Write(dectyptedBytes, 0, dectyptedBytes.Length);
                        //byte[] bytes = new byte[part.Length];
                        //int readBytes = part.Read(bytes, 0, bytes.Length);
                        //output.Write(bytes, 0, readBytes);
                    }
                }
            }
        }
    }

    static void Main()
    {
        string pathToFile = "Mecho.flv";
        Console.Write("Enter passowrd: ");
        string password = Console.ReadLine();
        SplitToParts(pathToFile, password);
        CombineParts(pathToFile, password);
    }
}
